using System;
using System.Collections.Generic;

namespace SG3L_RAG.Domain.Entities
{
    public class Document : EntityBase
    {
        // 文档内容转向量（伪实现，实际应调用AI嵌入服务）
        public float[] ToVector(string embeddingService = "default")
        {
            // 此处应调用实际的嵌入服务，如OpenAI、HuggingFace等
            // 示例：return EmbeddingClient.GetVector(Content, embeddingService);
            // 当前仅返回空向量，供后续扩展
            return new float[0];
        }
        public string Name { get; private set; } = string.Empty;
        public string FileName { get; private set; } = string.Empty;
        public string Type { get; private set; } = string.Empty;
        public long Size { get; private set; }
        public Guid UploaderId { get; private set; }
        public User? Uploader { get; private set; }
        public string Status { get; private set; } = string.Empty;
        public string Permission { get; private set; } = string.Empty;
        public bool IsProcessed { get; private set; } = false;
        private readonly List<DocumentChunk> _chunks = new();
        public IReadOnlyCollection<DocumentChunk> Chunks => _chunks.AsReadOnly();
        private readonly List<Permission> _permissions = new();
        public IReadOnlyCollection<Permission> Permissions => _permissions.AsReadOnly();

        // EF Core 需要的无参构造函数
        public Document() { }

        // 业务构造函数
        private Document(string name, string fileName, string type, long size, Guid uploaderId, string status = "pending", string permission = "private")
        {
            Name = name;
            FileName = fileName;
            Type = type;
            Size = size;
            UploaderId = uploaderId;
            Status = status;
            Permission = permission;
            IsProcessed = false;
            CreatedAt = DateTime.UtcNow;
        }

        // 工厂方法
        public static Document Create(string name, string fileName, string type, long size, Guid uploaderId, string status = "pending", string permission = "private")
        {
            return new Document(name, fileName, type, size, uploaderId, status, permission);
        }

        // 新的工厂方法，保持向后兼容
        public static Document Create(string name, string type, Guid uploaderId, string status, string permission)
        {
            return new Document(name, name, type, 0, uploaderId, status, permission);
        }        public bool IsPublic() => Permission == "public";

        // 支持的文档格式判断
        public bool IsText() => Type == "txt" || Type == "md";
        public bool IsOffice() => Type == "pdf" || Type == "doc" || Type == "docx" || Type == "ppt" || Type == "pptx" || Type == "xls" || Type == "xlsx";
        public bool IsImage() => Type == "png" || Type == "jpg" || Type == "jpeg";

        // 权限设置
        public void SetPermission(string permission)
        {
            Permission = permission;
        }

        // 处理状态
        public void SetStatus(string status)
        {
            Status = status;
        }

        public void SetProcessed()
        {
            IsProcessed = true;
            Status = "processed";
        }

        public bool IsPending() => Status == "pending";

        // 批量上传支持
        public static List<Document> BatchCreate(List<(string name, string type, Guid uploaderId, string status, string permission)> docs)
        {
            var result = new List<Document>();
            foreach (var d in docs)
            {
                result.Add(Document.Create(d.name, d.type, d.uploaderId, d.status, d.permission));
            }
            return result;
        }

        // 向量化处理（示例：添加分片）
        public void AddChunk(DocumentChunk chunk)
        {
            _chunks.Add(chunk);
        }

        // 权限管理
        public void AddPermission(Permission perm)
        {
            _permissions.Add(perm);
        }
    }
}
